<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="4">
                <el-card>
                    <div class="title">
                        <div class="round">
                            <el-icon>
                                <ChatLineSquare />
                            </el-icon>
                        </div>
                        <h4 class="ml">今日总收入（元）</h4>
                    </div>
                    <div class="percent">
                        <h1>1,234,567</h1>
                        <div>24%</div>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="4">
                <el-card>
                    <div class="title">
                        <div class="round">
                            <el-icon>
                                <ChatLineSquare />
                            </el-icon>
                        </div>
                        <h4 class="ml">会员卡储值金额（元）</h4>
                    </div>
                    <div class="percent">
                        <h1>{{ formatNumberToThounsands(123456781) }}</h1>
                        <div>-16%</div>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="4">
                <el-card>
                    <div class="title">
                        <div class="round">
                            <el-icon>
                                <ChatLineSquare />
                            </el-icon>
                        </div>
                        <h4 class="ml">本月总收入（万元）</h4>
                    </div>
                    <div class="percent">
                        <h1>2,924</h1>
                        <div>-24%</div>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="4">
                <el-card>
                    <div class="title">
                        <div class="round">
                            <el-icon>
                                <ChatLineSquare />
                            </el-icon>
                        </div>
                        <h4 class="ml">服务费总金额（元）</h4>
                    </div>
                    <div class="percent">
                        <h1>16,824</h1>
                        <div>-7%</div>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="4">
                <el-card>
                    <div class="title">
                        <div class="round">
                            <el-icon>
                                <ChatLineSquare />
                            </el-icon>
                        </div>
                        <h4 class="ml">停车费总金额（元）</h4>
                    </div>
                    <div class="percent">
                        <h1>9,687</h1>
                        <div>-4%</div>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="4">
                <el-card>
                    <div class="title">
                        <div class="round">
                            <el-icon>
                                <ChatLineSquare />
                            </el-icon>
                        </div>
                        <h4 class="ml">电费总金额（元）</h4>
                    </div>
                    <div class="percent">
                        <h1>223,674</h1>
                        <div>-19%</div>
                    </div>
                </el-card>
            </el-col>
        </el-row>
        <el-card class="mt">
            <div ref="cobinationChart" style="width: 100%;height: 300px;"></div>
        </el-card>
        <el-card class="mt">
            <el-input v-model.trim="name" style="max-width: 400px" placeholder="请输入站点名称">
                <template #append>
                    <el-button @click="loadData" icon="Search" />
                </template>
            </el-input>
            <el-table :data="tableData" style="width: 100%;" v-loading="loading">
                <el-table-column type="index" label="序号" width="80"></el-table-column>
                <el-table-column label="充电站名称" prop="name" width="180"></el-table-column>
                <el-table-column label="充电站ID" prop="id"></el-table-column>
                <el-table-column label="所属城市" prop="city"></el-table-column>
                <el-table-column label="充电桩总量(个)" prop="count" width="200">
                    <template #default="scope">
                        <span>{{ scope.row.count }}</span>
                        <el-tag class="ml" :type="scope.row.fault > 0 ? 'danger' : 'success'">
                            {{ scope.row.fault > 0 ? `故障数量为:${scope.row.fault}` : '次站点暂无发现故障' }}
                        </el-tag>
                    </template>
                </el-table-column>

                <el-table-column label="电费营收(万元)" prop="electricity"></el-table-column>
                <el-table-column label="停车费营收(元)" prop="parkingFee"></el-table-column>
                <el-table-column label="服务费营收(元)" prop="serviceFee"></el-table-column>
                <el-table-column label="会员储值金(元)" prop="member"></el-table-column>

                <el-table-column label="单日总收入(元)" prop="day">
                    <template #default="scope">
                        <span>{{ scope.row.day }}</span>
                        <el-tag class="ml" :type="scope.row.percent >= 0 ? 'success' : 'danger'">
                            {{ scope.row.percent >= 0 ? '+' + scope.row.percent + '%' : scope.row.percent + '%' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="月度总收入(元)" prop="month">
                    <template #default="scope">
                        <span>{{ scope.row.month }}</span>
                        <el-tag class="ml" :type="scope.row.mpercent >= 0 ? 'success' : 'danger'">
                            {{ scope.row.mpercent >= 0 ? '+' + scope.row.mpercent + '%' : scope.row.mpercent + '%' }}
                        </el-tag>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination class="mt mb fr" v-model:current-page="pageInfo.page" v-model:page-size="pageInfo.pageSize"
                :page-sizes="[10, 20, 30, 40]" background layout="total, sizes, prev, pager, next, jumper"
                :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </el-card>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue'
import { formatNumberToThounsands } from '@/utils/toThousands'
import { getRevenueChartAPI, getRevenueListAPI, getStationListAPI } from '@/api/chargingstation'
import { UserChart } from '@/hook/useChart'
import { UserPagination } from '@/hook/usePagination'

const name = ref<string>('')
// 获取图表
const cobinationChart = ref(null)
// table数据
const tableData = ref<any[]>([])
// 表单数据加载
const loading = ref<boolean>(false)
const setCombinationChart = async () => {
    const chartOptions: any = reactive({
        tooltip: {
            trigger: 'axis',

        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        legend: {

            top: 10
        },
        xAxis: [
            {
                type: 'category',
                data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月'],
                axisTick: {
                    alignWithLabel: true
                }
            }
        ],
        yAxis: [
            {
                type: 'value',
                name: "销售"
            },
            {
                type: 'value',
                name: "访问量"
            }
        ],
        series: [
            {
                name: '销售',
                type: 'bar',
                barWidth: '60%',
                data: [],
                color: '#409eff',
                yAxisIndex: 0
            },
            {
                name: '访问量',
                data: [],
                type: 'line',
                smooth: true,
                color: '#409eff',
                yAxisIndex: 1
            }
        ]
    })
    const res = await getRevenueChartAPI()
    for (var i = 0; i < res.data.list.length; i++) {
        chartOptions.series[i].name = res.data.list[i].name
        chartOptions.series[i].data = res.data.list[i].data
    }
    return chartOptions
}
UserChart(cobinationChart, setCombinationChart)
// table数据1
const loadData = async () => {
    loading.value = true
    const res = await getRevenueListAPI({ name: name.value, ...pageInfo })
    let res2 = await loadData2()
    loading.value = false
    tableData.value = res.data.list
    tableData.value = tableData.value.map((item) => ({
        ...item,
        day: item.electricity + item.member + item.parkingFee + item.serviceFee,
        fault: res2.filter((i: any) => i.name === item.name)[0].fault
    }))
    console.log(tableData.value, 'tableData.value')
    setTotal(res.data.total)
}
const loadData2 = async () => {
    const res = await getStationListAPI({ status: 1, ...pageInfo })
    return res.data.list
}
const { total, pageInfo, handleCurrentChange, handleSizeChange, setTotal } = UserPagination(loadData)
onMounted(() => {
    loadData()
})
</script>


<style lang="less" scoped>
.title {
    display: flex;
    align-items: center;

    .round {
        height: 30px;
        width: 30px;
        background-color: #ebecf5;
        text-align: center;
        line-height: 30px;
        border-radius: 100%;
    }

    h4 {
        margin-right: 20px;
        color: #7d706b;
    }
}

.percent {
    display: flex;
    align-items: center;

    h1 {
        margin-top: 20px;
        font-size: 28px;
    }

    div {
        margin-top: 20px;
        margin-left: 20px;
        width: 20px;
        height: 5px;
        text-align: center;
        background-color: #ebf7ef;
        padding: 8px;
        font-size: 12px;
        line-height: 5px;
        color: green;
    }
}
</style>